package com.ruoyi.js.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.js.domain.JsItem;
import com.ruoyi.js.service.IJsItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小项Controller
 * 
 * @author greejoy
 * @date 2024-04-22
 */
@Controller
@RequestMapping("/js/item")
public class JsItemController extends BaseController
{
    private String prefix = "js/item";

    @Autowired
    private IJsItemService jsItemService;

    @RequiresPermissions("js:item:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询小项列表
     */
    @RequiresPermissions("js:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@CookieValue("app_match_id") Long matchId, JsItem jsItem)
    {
        jsItem.setMatchId(matchId);
        startPage();
        List<JsItem> list = jsItemService.selectJsItemList(jsItem);
        return getDataTable(list);
    }

    /**
     * 导出小项列表
     */
    @RequiresPermissions("js:item:export")
    @Log(title = "小项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@CookieValue("app_match_id") Long matchId, JsItem jsItem)
    {
        jsItem.setMatchId(matchId);
        List<JsItem> list = jsItemService.selectJsItemList(jsItem);
        ExcelUtil<JsItem> util = new ExcelUtil<JsItem>(JsItem.class);
        return util.exportExcel(list, "item");
    }

    /**
     * 新增小项
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存小项
     */
    @RequiresPermissions("js:item:add")
    @Log(title = "小项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@CookieValue("app_match_id") Long matchId, JsItem jsItem)
    {
        jsItem.setMatchId(matchId);
        return toAjax(jsItemService.insertJsItem(jsItem));
    }

    /**
     * 修改小项
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JsItem jsItem = jsItemService.selectJsItemById(id);
        mmap.put("jsItem", jsItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存小项
     */
    @RequiresPermissions("js:item:edit")
    @Log(title = "小项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JsItem jsItem)
    {
        return toAjax(jsItemService.updateJsItem(jsItem));
    }

    /**
     * 删除小项
     */
    @RequiresPermissions("js:item:remove")
    @Log(title = "小项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jsItemService.deleteJsItemByIds(ids));
    }
}
